package com.smart.manager.pop.repository;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.RandomUtil;
import com.smart.common.entities.core.Page;
import com.smart.manager.pop.entities.body.RDisBill;
import com.smart.manager.pop.entities.enums.BillKind;
import com.smart.manager.pop.entities.enums.BillState;
import com.smart.manager.pop.entities.models.PopDisBill;
import com.smart.manager.pop.entities.models.query.QPopDisBill;
import com.smart.manager.pop.entities.views.InviterView;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 支付中心-订单管理
 *
 * @author Administrator
 */

@Log4j2
@Service
@RequiredArgsConstructor
public class DisBillRepository {


    public Page<PopDisBill> pageList(RDisBill.SearchBody search) {
        QPopDisBill query = new QPopDisBill();
        Opt.ofBlankAble(search.getKind()).ifPresent(x->query.kind.eq(x)) ;
        Opt.ofBlankAble(search.getStatus()).ifPresent(x->query.status.eq(x)) ;
        Opt.ofNullable(search.getAgentPhone()).ifPresent(x->query.agent.phone.contains(x)) ;
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).order().id.desc();
        return new Page<PopDisBill>().load(query.findPagedList());
    }






    /**
     * 代理流水列表
     * @param agentId 经销商代理
     * @param search           搜索
     * @return {@link List}<{@link PopDisBill}>
     */
    public List<PopDisBill> agentList(Long agentId, RDisBill.SearchH5 search) {
        QPopDisBill query = new QPopDisBill();
        LocalDateTime now = LocalDateTime.now();
        int month = search.getMonth();
        if (search.getMonth() <= 0 ){
            month = now.getDayOfMonth();
        }
        LocalDateTime kais = LocalDateTime.now().withMonth(month).withDayOfMonth(1);
        LocalDateTime start = LocalDateTimeUtil.beginOfDay(LocalDateTime.now().withMonth(month).withDayOfMonth(1));
        LocalDateTime ending = LocalDateTimeUtil.endOfDay(start.plusMonths(1).minusDays(1));

        log.warn("start = {}",start);
        log.warn("ending = {}",ending);
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        query.agent.id.eq(agentId);
        query.createTime.between(start, ending).order().id.desc();
        return query.findList();
    }
    
    
    

    public void puxData(InviterView inviterView, BillState billState, BillKind billKind, BigDecimal amount, String remark){
        new PopDisBill()
                .setCode(generateTradeNo()).setKind(billKind).setStatus(billState).setAmount(amount).setAgent(inviterView).setRemark(remark).insert();
    }


    public BigDecimal getDay(Long inviter){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dateTime = now.withMinute(0).withHour(0).withSecond(0).withNano(0);
        List<BigDecimal> attributeList = new QPopDisBill().agent.id.eq(inviter).createTime.between(dateTime, now).kind.ne(BillKind.SYSTEM).kind.ne(BillKind.RECEIPT).status.eq(BillState.REVENUE).select(QPopDisBill.Alias.amount).findSingleAttributeList();
        BigDecimal zero = BigDecimal.ZERO;
        for (BigDecimal decimal : attributeList) {
            zero = zero.add(decimal);
        }
        return zero;
    }
    /**
     * 生成订单id 长度为25个随机字符串
     *
     * @return {@code String}
     */
    private String generateTradeNo() {
        String x = DateUtil.format(new Date(), "yyyyMMddHHmmss").concat(RandomUtil.randomNumbers(5));
        return "TZ".concat(x);
    }

}
